const order = require('../model/orderModel.js');
const { off } = require('../tool/db.js');

async function add(ctx){
    //订单号
    let number = Date.now() + parseInt(Math.random() * 1000).toString()
    //用户id
    let uid = ctx.user.id
    //商品信息
    let gdata = ctx.request.body.gdata
    gdata = JSON.parse(gdata)
    if(!Array.isArray(gdata)){
        gdata = [gdata]
    }
    //地址
    let address = ctx.request.body.address
    //金额
    let price = 0
    for (const i of gdata) {
        price += i.gcount * i.gprice
    }
    if(!price){
        return ctx.body = {code :0 ,msg:'金额有误',data:''}
    }
    if(!address){
        return ctx.body = {code:0,msg:'请选择地址',data:''}
    }
    //优惠券
    let couponid = ctx.request.body.couponid

    let price2 = price
    if(couponid){
        price2 = await order.coupon({couponid,price,uid})
    }
    if(price2.data == price){
        return ctx.body = {code:0,msg:price2.msg,data:''}
    }
    let res = await order.add({number,uid,gdata,address,price,price2:price2.data,couponid})

    ctx.body= res
}

async function adminList(ctx){
    let id = parseInt(ctx.query.id)
    let page = parseInt(ctx.query.page) || 1
    let num = parseInt(ctx.query.num) ||100000
    let offset = (page -1 ) * num

    let res = await order.adminList({id,num,offset})

    // if(!Array.isArray(res)){
    //     res = [res]
    // }
    if(!res){
        return ctx.body = {code:0,msg:'没有符合条件的订单',data:''}
    }
    let xx = {}
    for (const i of res) {
        if(xx[i.number]){
            xx[i.number].children.push(JSON.parse(JSON.stringify(i)))
        }else{
            xx[i.number] = i
            xx[i.number].children = [JSON.parse(JSON.stringify(i))]
        }
    }

    ctx.body = {code:1,msg:'成功',data:xx}
}

async function adminMod(ctx){
    let orderid = ctx.request.body.orderid
    let id = parseInt(ctx.request.body.id)
    let adminID = ctx.user.id
    let userid = ctx.request.body.userid
    if( id == 1 || id == 4){
        
    let res = await order.adminMod({id,orderid,adminID,userid})
    ctx.body = res
    }else{
        return ctx.body = {code:0,msg:'参数有误',data:''}
    }
    
}

async function userList(ctx){
    let uid = ctx.user.id
    let id = parseInt(ctx.query.id)
    if(!id || id == 1 || id == 2 || id == 4 || id == 5  ){
        let res = await order.userList({uid,id})

        // if(!Array.isArray(res)){
        //     res = [res]
        // }
        if(!res){
            return ctx.body = {code:0,msg:'没有符合条件的订单',data:''}
        }
        let xx = {}
        for (const i of res) {
            if(xx[i.number]){
                xx[i.number].children.push(JSON.parse(JSON.stringify(i)))
            }else{
                xx[i.number] = i
                xx[i.number].children = [JSON.parse(JSON.stringify(i))]
            }
        }
    
        ctx.body = {code:1,msg:'成功',data:xx}
    }else{
        return ctx.body = {code:0,msg:'参数错误',data:''}
    }
    
}

async function userMod(ctx){
    let id = parseInt(ctx.request.body.id)
    let orderid = ctx.request.body.orderid

    if(id == 2 || id == 3){
        let res = await order.userMod({id,orderid})
        return ctx.body = res
        
    }
    
    return ctx.body = {code:0,msg:'参数错误',data:''}
}
module.exports = { add , adminList,adminMod,userList,userMod}